package service

import (
	"html/template"
	"net/http"
)

func StaticHandler() http.Handler {
	return http.StripPrefix("/static/", http.FileServer(http.Dir("./static")))
}

func Favicon(response http.ResponseWriter, request *http.Request) {
	http.ServeFile(response, request, "./static/favicon.ico")
}

func Index(response http.ResponseWriter, request *http.Request) {
	if !(request.URL.Path == "/" && request.Method == "GET") {
		http.Error(response, "page not found, "+request.URL.Path, http.StatusNotFound)
		return
	}
	params := request.URL.Query()
	var page *template.Template
	var err error
	if len(params) > 0 {
		page, err = template.ParseFiles("./templates/index.html")
	} else {
		page, err = template.ParseFiles("./templates/client.html")
	}
	if err != nil {
		http.Error(response, err.Error(), http.StatusInternalServerError)
		return
	}
	data := map[string]string{
		"title": "File Rename",
	}
	err = page.Execute(response, data)
	if err != nil {
		http.Error(response, err.Error(), http.StatusInternalServerError)
	}
}
